package log

import (
	"AnniversaryNoticeLite/pkg/config"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"gopkg.in/natefinch/lumberjack.v2"
	"os"
	"path"
)

var Logger *zap.Logger

func InitLogger() {
	level := zap.NewAtomicLevelAt(zapcore.DebugLevel)
	lumberjackLogger := &lumberjack.Logger{
		Filename:   path.Join(config.Configs.Log.Dir, "app.log"),
		MaxSize:    50,
		MaxAge:     7,
		MaxBackups: 10,
		Compress:   true,
		LocalTime:  true,
	}
	fileEncoder := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
	fileWriter := zapcore.AddSync(lumberjackLogger)
	coreFile := zapcore.NewCore(fileEncoder, zapcore.AddSync(fileWriter), level)

	consoleEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
	consoleWriter := zapcore.Lock(os.Stdout)
	coreConsole := zapcore.NewCore(consoleEncoder, consoleWriter, level)
	Logger = zap.New(zapcore.NewTee(coreConsole, coreFile), zap.AddCaller())
}
